package com.comba.nutz.boot.auth.server;

import com.comba.nutz.boot.auth.server.entity.AccountEntity;
import com.comba.nutz.boot.auth.server.entity.DataDictionaryEntity;
import com.comba.nutz.boot.auth.server.service.impl.AccountServiceImpl;
import com.comba.nutz.boot.starter.dao.CndPlus;
import org.nutz.dao.pager.Pager;
import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by zhangjunpeng on 2018/5/22.
 */
@RestController
@SpringBootApplication
@ComponentScan(value = "com.comba")
public class NutzBootAuthServerApplicaton {

    @Autowired
    ConsumerTokenServices tokenServices;

    @Autowired
    AccountServiceImpl accountService;

    @GetMapping("test")
    public String test() {
        AccountEntity accountEntity = accountService.fetch("2c92808263a61ff00163aacda7da00aa");
        accountEntity.setStatus("1");
        accountService.updateAccount(accountEntity);

        return Json.toJson(accountService.queryByJoin(CndPlus.where("code_value", "用户"), "accountType", new Pager(1, 5)));
        //return Json.toJson(accountService.fetch("2c908c8e63a9a1850163a9a892a4000b"));
    }

    @GetMapping("testUpdate")
    public String testUpdate() {
        AccountEntity accountEntity = accountService.fetchByJoin("123456", "accountType");
        accountEntity.getAccountType().setCodeValue("test1");

        return Json.toJson(accountService._updateWith(accountEntity, "accountType"));
    }

    @GetMapping("testInsert")
    public String testInsert() {
        AccountEntity accountEntity = new AccountEntity();
        accountEntity.setId("123456");
        accountEntity.setUsername("test1");
        accountEntity.setUserNo("test1");

        DataDictionaryEntity dataDictionaryEntity = new DataDictionaryEntity();
        dataDictionaryEntity.setId("1234");
        dataDictionaryEntity.setCodeKey("test");
        dataDictionaryEntity.setCodeValue("test");
        dataDictionaryEntity.setEnabled(true);
        accountEntity.setAccountType(dataDictionaryEntity);

        return Json.toJson(accountService._insertWith(accountEntity, "accountType"));
    }

    @GetMapping("/revokeToken")
    @ResponseBody
    public String revokeToken(String access_token) {
        tokenServices.revokeToken(access_token);

        return "logout success";
    }

    public static void main(String args[]) {
        SpringApplication.run(NutzBootAuthServerApplicaton.class, args);
    }
}
